package com.zhkj.shixi.tvfilemanager;

import android.app.Activity;
import android.util.Log;

import com.zhkj.shixi.tvfilemanager.Interface.SearchFile;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import static com.zhkj.shixi.tvfilemanager.StaticClasses.fileManager;
import static com.zhkj.shixi.tvfilemanager.utils.PinYinUtil.getPingYin;


/**
 * Created by chenhao on 19-8-12.
 */
public class FileSearch implements SearchFile {
    private Activity mActivity;
    private static final String TAG = "FileSearch";
//    public final static int UPDATE_UI =1;
    private List<File> mFileList;
    private int type;


    public FileSearch(Activity activity) {
        mActivity =activity;
        mFileList =new ArrayList<File>();
        type = fileManager.getType();
    }


    @Override
    public void fileFilter(final String path) {
        mFileList.clear();
        Log.i(TAG, "fileFilter: "+path);
        Thread thread =null;
        thread=new  Thread(new Runnable() {
            @Override
            public void run() {
                fileFilterF(path);
                Log.i(TAG, "run: "+mFileList.size());

            }
        });
        thread.start();
//        try {
//            thread.join();
//        } catch (InterruptedException e){
//            e.printStackTrace();
//        }

    }

    public void fileFilterF(final String path){
        List<File> files = FileManager.getTypeFiles(path,type);
        for (File fileIndex : files) {
            //如果这个文件是目录，则进行递归搜索
            if (fileIndex.isDirectory()) {
                fileFilterF(fileIndex.getAbsolutePath());
            }
            else{
                mFileList.add(fileIndex);
            }
        }
    }
    /**
     *
     * @param keyword 搜索关键字
     * @return 搜索结果
     */
    @Override
    public List<File> fileSearch(String keyword) {
        List<File> f=new ArrayList<>();
        for (File file:mFileList){
            Log.w("TAG",file.getName());
            if (getPingYin(file.getName()).contains(keyword)){
                f.add(file);
            }
        }
        return f;
    }
    public List<File> getAllMediaFiles(){
        return this.mFileList;
    }


}



